package com.kun.controller;

import com.kun.common.domain.AjaxResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

/**
 * web层通用数据处理
 */
@Slf4j
public class BaseController {

    /**
     * 返回成功
     */
    public AjaxResult success() {
        return AjaxResult.success(null);
    }

    /**
     * 返回失败消息
     */
    public AjaxResult error() {
        return AjaxResult.error();
    }

    /**
     * 返回成功消息
     */
    public ResponseEntity<AjaxResult> success(String message) {
        AjaxResult success = AjaxResult.success((Object) message);
        return new ResponseEntity<>(success, HttpStatus.OK);
    }

    /**
     * 返回成功消息
     */
    public ResponseEntity<AjaxResult> success(Object o) {
        AjaxResult success = AjaxResult.success(o);
        return new ResponseEntity<>(success, HttpStatus.OK);
    }

    /**
     * 返回成功消息
     */
    public ResponseEntity<AjaxResult> success(Boolean b) {
        return new ResponseEntity<>(toAjax(b), b ? HttpStatus.OK : HttpStatus.INTERNAL_SERVER_ERROR);
    }


    /**
     * 响应返回结果
     */
    protected AjaxResult toAjax(boolean result) {
        return result ? success() : error();
    }


}
